// pages/evaluation/evaluation.js
const utils = require('../../utils/util.js');
const uploadImage = require('../../utils/uploadFile.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsMarkInfo: null,
        goodsMark: [],
        flag: 5,
        content: '',
        imageList: [],
        current: '', // 当前显示图片的http链接
        urls: [] // 需要预览的图片http链接列表
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            goodsID: options.goodsID,
            orderNo: options.orderNo,
            merchantID: options.merchantID,
        });
        this.getALiOSSData();
        this.getGoodsMarkData(options.goodsID, options.orderNo);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    changeColor1: function () {
        var that = this;
        that.setData({
            flag: 1
        });
    },
    changeColor2: function () {
        var that = this;
        that.setData({
            flag: 2
        });
    },
    changeColor3: function () {
        var that = this;
        that.setData({
            flag: 3
        });
    },
    changeColor4: function () {
        var that = this;
        that.setData({
            flag: 4
        });
    },
    changeColor5: function () {
        var that = this;
        that.setData({
            flag: 5
        });
    },
    checkbox: function (e) {
        var index = e.currentTarget.dataset.index;//获取当前点击的下标
        var _goodsMark = this.data.goodsMarkInfo.goodsMark;//选项集合
        _goodsMark[index].checked = !_goodsMark[index].checked;//改变当前选中的checked值
        this.setData({
            goodsMark: _goodsMark
        });
    },
    checkboxChange: function (e) {
        var checkValue = e.detail.value;
        this.setData({
            checkValue: checkValue
        });
    },
    bindInput: function (e) {
        this.setData({
            content: e.detail.value
        })
    },
    //选择照片上传
    addImage: function () {
        var that = this;
        wx.chooseImage({
            count: 9, // 默认最多一次选择9张图
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                var nowTime = utils.formatDate(new Date());

                //支持多图上传
                for (var i = 0; i < res.tempFilePaths.length; i++) {
                    //显示消息提示框
                    wx.showLoading({
                        title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
                        mask: true
                    })

                    //上传图片
                    //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
                    //图片路径可自行修改
                    uploadImage(res.tempFilePaths[i], 'miniZXU/',
                        function (result) {
                            var _imageList = that.data.imageList;
                            _imageList.push(result);
                            that.setData({
                                imageList: _imageList
                            });
                            wx.hideLoading();
                        }, function (result) {
                            wx.hideLoading();
                        }
                    )
                }
            }
        })
    },
    previewImage: function (e) {
        var list = e.currentTarget.dataset.list;
        var index = e.currentTarget.dataset.index;
        wx.previewImage({
            current: list[index], // 当前显示图片的http链接
            urls: list // 需要预览的图片http链接列表
        })
    },
    deleteImage: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var list = e.currentTarget.dataset.list;
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除图片吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#999999',
            confirmText: '确定',
            confirmColor: '#1DA9FC',
            success: function (res) {
                if (res.confirm) {
                    list.splice(index, 1);
                    that.setData({
                        imageList: list
                    });
                }
            },
            fail: function (res) {

            },
            complete: function (res) { },
        })
    },
    getALiOSSData: function () {
        var that = this;
        var params = {
            userID: app.globalData.userID
        }
        app.network.requestPost(app.route.ROUTE_GET_ALIOSS, params, '', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                app.globalData['aliOSS'] = res.context;
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    getGoodsMarkData: function (_goodsID, _orderNo) {
        var that = this;
        var params = {
            userID: app.globalData.userID,
            goodsID: _goodsID,
            orderNo: _orderNo
        }
        app.network.requestPost(app.route.ROUTE_GET_GOODS_MARK, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                that.setData({
                    goodsMarkInfo: res.context,
                    goodsMark: res.context.goodsMark
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    evaluateTap: function(e) {
        var that = this;
        var _markIds = '';
        for (var item of that.data.goodsMark) {
            if (item.checked) {
                _markIds += item.markID;
                _markIds += ",";
            }
        }
        _markIds = _markIds.substring(0, _markIds.length - 1);
        var params = {
            userID: app.globalData.userID,
            goodsID: that.data.goodsID,
            orderNo: that.data.orderNo,
            merchantID: that.data.merchantID,
            goodsStars: that.data.flag,
            markIds: _markIds,
            goodsContent: that.data.content,
            evalusteImgs: that.data.imageList.toString()
        }
        app.network.requestPost(app.route.ROUTE_EVALUATE_GOODS, params, '正在提交评价', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                wx.redirectTo({
                    url: '../order/order?tab=0',
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    }
})